package com.hframe.utils;

/**
 * 常用数据类型和byte[]的转换
 * 
 * @author Administrator
 *
 */
public class BytesUtils {

	/**
	 * 从short值获得byte数据
	 * 
	 * @param num
	 * @return byte数据
	 */
	public static byte[] getByte(short num) {
		byte[] ret = new byte[2];
		putShort(ret, num, 0);
		return ret;
	}

	/**
	 * 将 short值压入一个byte数组中
	 * 
	 * @param b
	 *            byte数组
	 * @param num
	 *            short值
	 * @param index
	 *            数据的起始位置
	 */
	public static void putShort(byte b[], short num, int index) {
		b[index] = (byte) (num & 0xff);
		b[index + 1] = (byte) ((num >> 8) & 0xff);
	}

	/**
	 * 从byte数组中获得short值
	 * 
	 * @param b
	 *            byte数组
	 * @return 返回short
	 */
	public static short getShort(byte[] b) {
		return getShort(b, 0);
	}

	/**
	 * 从byte数组指定位置获得short值
	 * 
	 * @param b
	 *            byte数组
	 * @param index
	 *            数据的起始位置
	 * @return 返回short
	 */
	public static short getShort(byte[] b, int index) {
		return (short) ((b[index] & 0xff) | (b[index + 1] & 0xff) << 8);
	}

	// /////////////////////////////////////////////////////////////

	/**
	 * 从int值获得byte数据
	 * 
	 * @param num
	 * @return byte数据
	 */
	public static byte[] getByte(int num) {
		byte[] ret = new byte[4];
		putInt(ret, num, 0);
		return ret;
	}

	/**
	 * 将 int值压入一个byte数组中
	 * 
	 * @param b
	 *            byte数组
	 * @param num
	 *            int值
	 * @param index
	 *            数据的起始位置
	 */
	public static void putInt(byte b[], int num, int index) {
		b[index] = (byte) (num & 0xff);
		b[index + 1] = (byte) ((num >> 8) & 0xff);
		b[index + 2] = (byte) ((num >> 16) & 0xff);
		b[index + 3] = (byte) ((num >> 24) & 0xff);
	}

	/**
	 * 从byte数组中获得int值
	 * 
	 * @param b
	 *            byte数组
	 * @return 返回int
	 */
	public static int getInt(byte[] b) {
		return getInt(b, 0);
	}

	/**
	 * 从byte数组指定位置获得int值
	 * 
	 * @param b
	 *            byte数组
	 * @param index
	 *            数据的起始位置
	 * @return 返回int
	 */
	public static int getInt(byte[] b, int index) {
		return (b[index] & 0xff) | (b[index + 1] & 0xff) << 8 | (b[index + 2] & 0xff) << 16 | (b[index + 3] & 0xff) << 24;
	}

	// /////////////////////////////////////////////////////////////
	/**
	 * 从long值获得byte数据
	 * 
	 * @param num
	 * @return byte数据
	 */
	public static byte[] getByte(long num) {
		byte[] ret = new byte[8];
		putLong(ret, num, 0);
		return ret;
	}

	/**
	 * 将 long值压入一个byte数组中
	 * 
	 * @param b
	 *            byte数组
	 * @param num
	 *            long值
	 * @param index
	 *            数据的起始位置
	 */
	public static void putLong(byte b[], long num, int index) {
		b[index] = (byte) (num & 0xff);
		b[index + 1] = (byte) ((num >> 8) & 0xff);
		b[index + 2] = (byte) ((num >> 16) & 0xff);
		b[index + 3] = (byte) ((num >> 24) & 0xff);
		b[index + 4] = (byte) ((num >> 32) & 0xff);
		b[index + 5] = (byte) ((num >> 40) & 0xff);
		b[index + 6] = (byte) ((num >> 48) & 0xff);
		b[index + 7] = (byte) ((num >> 56) & 0xff);
	}

	/**
	 * 从byte数组中获得long值
	 * 
	 * @param b
	 *            byte数组
	 * @return 返回long
	 */
	public static long getLong(byte[] b) {
		return getLong(b, 0);
	}

	/**
	 * 从byte数组指定位置获得long值
	 * 
	 * @param b
	 *            byte数组
	 * @param index
	 *            数据的起始位置
	 * @return 返回long
	 */
	public static long getLong(byte[] b, int index) {
		return (b[index] & 0xff) | ((long) (b[index + 1] & 0xff)) << 8 | ((long) (b[index + 2] & 0xff)) << 16 | ((long) (b[index + 3] & 0xff)) << 24 | ((long) (b[index + 4] & 0xff)) << 32 | ((long) (b[index + 5] & 0xff)) << 40 | ((long) (b[index + 6] & 0xff)) << 48
				| ((long) (b[index + 7] & 0xff)) << 56;
	}

	// /////////////////////////////////////////////////////////////
	/**
	 * 从float值获得byte数据
	 * 
	 * @param num
	 * @return byte数据
	 */
	public static byte[] getByte(float num) {
		byte[] ret = new byte[8];
		putFloat(ret, num, 0);
		return ret;
	}

	/**
	 * 将 float值压入一个byte数组中
	 * 
	 * @param b
	 *            byte数组
	 * @param num
	 *            float值
	 * @param index
	 *            数据的起始位置
	 */
	public static void putFloat(byte b[], float value, int index) {
		int num = Float.floatToIntBits(value);
		b[index] = (byte) (num & 0xff);
		b[index + 1] = (byte) ((num >> 8) & 0xff);
		b[index + 2] = (byte) ((num >> 16) & 0xff);
		b[index + 3] = (byte) ((num >> 24) & 0xff);
	}

	/**
	 * 从byte数组中获得float值
	 * 
	 * @param b
	 *            byte数组
	 * @return 返回float
	 */
	public static float getFloat(byte[] b) {
		return getFloat(b, 0);
	}

	/**
	 * 从byte数组指定位置获得float值
	 * 
	 * @param b
	 *            byte数组
	 * @param index
	 *            数据的起始位置
	 * @return 返回float
	 */
	public static float getFloat(byte[] b, int index) {
		int num = (b[index] & 0xff) | (b[index + 1] & 0xff) << 8 | (b[index + 2] & 0xff) << 16 | (b[index + 3] & 0xff) << 24;
		return Float.intBitsToFloat(num);
	}

	// /////////////////////////////////////////////////////////////
	/**
	 * 从double值获得byte数据
	 * 
	 * @param num
	 * @return byte数据
	 */
	public static byte[] getByte(double num) {
		byte[] ret = new byte[8];
		putDouble(ret, num, 0);
		return ret;
	}

	/**
	 * 将 double值压入一个byte数组中
	 * 
	 * @param b
	 *            byte数组
	 * @param num
	 *            double值
	 * @param index
	 *            数据的起始位置
	 */
	public static void putDouble(byte b[], double value, int index) {
		long num = Double.doubleToLongBits(value);
		b[index] = (byte) (num & 0xff);
		b[index + 1] = (byte) ((num >> 8) & 0xff);
		b[index + 2] = (byte) ((num >> 16) & 0xff);
		b[index + 3] = (byte) ((num >> 24) & 0xff);
		b[index + 4] = (byte) ((num >> 32) & 0xff);
		b[index + 5] = (byte) ((num >> 40) & 0xff);
		b[index + 6] = (byte) ((num >> 48) & 0xff);
		b[index + 7] = (byte) ((num >> 56) & 0xff);
	}

	/**
	 * 从byte数组中获得double值
	 * 
	 * @param b
	 *            byte数组
	 * @return 返回double
	 */
	public static double getDouble(byte[] b) {
		return getDouble(b, 0);
	}

	/**
	 * 从byte数组指定位置获得double值
	 * 
	 * @param b
	 *            byte数组
	 * @param index
	 *            数据的起始位置
	 * @return 返回double
	 */
	public static double getDouble(byte[] b, int index) {
		long num = (b[index] & 0xff) | ((long) (b[index + 1] & 0xff)) << 8 | ((long) (b[index + 2] & 0xff)) << 16 | ((long) (b[index + 3] & 0xff)) << 24 | ((long) (b[index + 4] & 0xff)) << 32 | ((long) (b[index + 5] & 0xff)) << 40 | ((long) (b[index + 6] & 0xff)) << 48
				| ((long) (b[index + 7] & 0xff)) << 56;
		return Double.longBitsToDouble(num);
	}
}
